package gao.xiaolei.dao;

import java.time.LocalDateTime;
import java.util.List;

import gao.xiaolei.entity.TestRequirement;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository("TestRequirementDao")
public interface TestRequirementDao extends
        JpaRepository<TestRequirement, String>,TreeMethod<TestRequirement> {

    // 根据项目id和状态和作者来查找测试需求的菜单字段（仅查找根节点）(只能获取自己写的和别人通过审核的)
    @Query(value = "select id,title,parentId,leaf,status,author from TestRequirement where project = :projectId and parentId='root' and (author=:userId or status=3) order by createDate")
    public List<Object[]> findByProject(@Param("projectId") String projectId,
                                        @Param("userId") String userId);

    // 根据项目id和状态来查找需求
    public List<TestRequirement> findByProjectAndStatus(String projectId, Integer status);

    // 根据状态和父节点来查找
    public List<TestRequirement> findByStatusAndParentId(Integer status, String parentId);

    // 根据状态来获取测试需求信息
    @Query(value = "select id,title,project from TestRequirement where status=:status order by createDate")
    public List<Object[]> findByStatus(@Param("status") Integer status);

    // 更改测试需求的某些信息
    @Modifying
    // update或者delete操作，在更新或者删除操作时，此注解必须加
    @Query(value = "update TestRequirement set title = :title,status=:status, updateDate=:updateDate where id = :id")
    public void updateForEditor(@Param("id") String id,
                                @Param("title") String title, @Param("status") int status,
                                @Param("updateDate") LocalDateTime updateDate);

    // 根据父测试需求Id获取测试需求信息的某些字段,用于形成树形结构菜单
    @Query("select id,title,parentId,leaf,status,author from TestRequirement r where r.parentId=:parentId order by createDate")
    public List<Object[]> querySomeByParentId(@Param("parentId") String parentId);


    // 根据项目id和状态和作者来查找测试需求的菜单字段（仅查找根节点）(只能获取自己写的和别人通过审核的)
    @Query(value = "select id,title,parentId,leaf from TestRequirement where project = :projectId and parentId='root' and status=3")
    public List<Object[]> findByProjectAndIsPass(@Param("projectId") String projectId);

    // 查找该节点的所有通过审核的子需求Id
    @Query(value = "select id from TestRequirement where parentId = :parentId and status=:status")
    public List<String> getChildrenIdByStatus(@Param("parentId") String parentId,
                                              @Param("status") Integer status);

    // 查找该节点的所有通过审核的子需求Id
    @Query(value = "select project from TestRequirement where id= :id ")
    public String getProject(@Param("id") String id);

}
